import axios from "axios"
import { ActionContext } from "vuex"
import { getMenu } from "/@/api/org/manager-service"
import { getUser, login } from "/@/api/sys"
import router from "/@/router"
import { setAccessToken } from "/@/utils/common"
import { routecompIdMapping, routeBreadcrumbMapping } from "/@/utils/compatible"
import { VmsMenu } from "/@/_types/sys/menu"
import { LoginUser, VmsUser } from "/@/_types/sys/user"

const state: VmsUser = {
    userId: null,
    userName: null,
    userAccount: null,
    userFullName: null,
    userImageUrl: null,
    deptCode: null,
    deptId: null,
    deptName: null,
    orgId: null,
    orgName: null,
    subOrgId: null,
    subOrgName: null,
    token: null,
    menus: [],
    //这两个后期可能放到别的状态里面去
    routeMappingMap: new Map(),
    routeBreadcrumbMappingMap: new Map(),
    firstMenusIndex: null
}

const actions = {
    //登录
    login(ctx: ActionContext<VmsUser, {}>, loginUser: LoginUser) {
        return new Promise((resolve, reject) => {
            login(loginUser).then(res => {
                let token = res.data.access_token
                ctx.commit('SET_USER_TOKEN', token)
                setAccessToken(token)
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        })
    },
    //登出
    logout(ctx: ActionContext<VmsUser, {}>) {
        ctx.commit('CLEAR_USER_TOKEN')
        sessionStorage.removeItem("Access-Token")
        router.push({ path: '/login' })
    },
    getInfo(ctx: ActionContext<VmsUser, {}>) {
        return new Promise((resolve, reject) => {
            axios.all([
                // getSysConfig(),//这个是拾贝云之前就有的 暂时用不到不做处理
                getUser(),//这个是获取用户信息
                getMenu()
            ]).then(axios.spread((userData: any, menuData: any) => {
                const user = userData.data, menus = menuData.data as Array<VmsMenu>
                //设置菜单
                ctx.commit("SET_USER_MENUS", menus)
                //设置用户信息
                ctx.commit("SET_USER_INFO", user)
                //设置部门信息
                ctx.commit("SET_ORG_DEPT_INFO", user)
                //设置系统信息 这个暂时不处理
                resolve(true)
            })).catch(error => {
                reject(error)
            })
        })
    },
    setMenus(ctx: ActionContext<VmsUser, {}>, menus: Array<VmsMenu>) {
        ctx.commit("SET_USER_MENUS", menus)
    },
    setToken(ctx: ActionContext<VmsUser, {}>, token: string) {
        ctx.commit("SET_USER_TOKEN", token)
    },
    clearToken(ctx: ActionContext<VmsUser, {}>) {
        ctx.commit("CLEAR_USER_TOKEN")
    },
    setFirstMenuIndex(ctx: ActionContext<VmsUser, {}>, index: string) {
        ctx.commit("SET_FIRST_MENU_INDEX", index)
    }
}

const mutations = {
    /**
     * 设置用户信息
     * @param state 
     * @param user 用户对象 
     * <pre>
     *  userId 用户id
     *  userAccount 用户账号
     *  userName 用户名
     *  userFullName 用户全名
     *  userImageUrl 用户头像地址
     * </pre>
     */
    SET_USER_INFO(state: VmsUser, user: any) {
        state.userId = user.userId
        state.userAccount = user.userAccount
        state.userName = user.username
        state.userFullName = user.userFullName
        state.userImageUrl = user.userImageUrl
    },
    /**
     * 清理用户信息
     * @param state 
     */
    CLEAR_USER_INFO(state: VmsUser) {
        state.userId = null
        state.userAccount = null
        state.userName = null
        state.userFullName = null
        state.userImageUrl = null
    },
    /**
     * 设置组织和部门信息
     * @param state 
     * @param dept 
     * <pre>
     *  orgId 组织id
     *  orgName 组织名称
     *  deptId 部门id
     *  deptCode 部门编码
     *  deptName 部门名称
     * </pre>
     */
    SET_ORG_DEPT_INFO(state: VmsUser, dept: any) {
        state.orgId = dept.orgId
        state.orgName = dept.orgName
        state.subOrgId = dept.subOrgId
        state.subOrgName = dept.subOrgName
        state.deptId = dept.deptId
        state.deptCode = dept.deptCode
        state.deptName = dept.deptName
    },
    /**
     * 清理组织和部门信息
     * @param state 
     */
    CLEAR_ORG_DEPT_INFO(state: VmsUser) {
        state.orgId = null
        state.orgName = null
        state.deptId = null
        state.deptCode = null
        state.deptName = null
    },
    /**
     * 设置令牌信息
     * @param state 
     * @param token 令牌
     */
    SET_USER_TOKEN(state: VmsUser, token: string) {
        state.token = token
    },
    /**
     * 清理令牌信息
     * @param state 
     */
    CLEAR_USER_TOKEN(state: VmsUser) {
        state.token = null
    },
    /**
     * 设置菜单信息
     * @param state 
     * @param menus 菜单
     */
    SET_USER_MENUS(state: VmsUser, menus: Array<VmsMenu>) {
        state.menus = menus
        state.routeMappingMap = routecompIdMapping(menus)
        state.routeBreadcrumbMappingMap = routeBreadcrumbMapping(menus)
    },
    /**
     * 设置一级菜单index
     * @param state 
     * @param index 
     */
    SET_FIRST_MENU_INDEX(state: VmsUser, index: string) {
        state.firstMenusIndex = index
    }
}

export default {
    namespaced: true,
    state,
    actions,
    mutations
}
